<template>
  <div class="app-container">
    <div class="table-operation">
      <span>
        <AuthButton @click="c" name="新增" />
      </span>
    </div>

    <el-table v-loading="list.loading" :data="list.data" v-bind="tableProp">
      <el-table-column align="center" type="index" />
      <el-table-column label="角色名称" prop="name" />
      <el-table-column label="备注" prop="remark" />
      <el-table-column align="center" label="操作">
        <template slot-scope="{ row }">
          <AuthButton
            @click="adminEmpower(row)"
            circle
            name="后台功能授权"
            type="success"
          >
            <i class="el-icon-s-check" />
          </AuthButton>
          <AuthButton @click="u(row, 'cache')" name="编辑" />
          <AuthButton @click="d({ id: row.roleId })" name="删除" />
        </template>
      </el-table-column>
    </el-table>

    <KiFormDialog
      :show.sync="form.show"
      :title="formTitle"
      v-model="form.data"
      :submit="submitForm"
      :readonly="form.status === 'r'"
      ref="formRef"
      :loading="form.loading"
    >
      <template #el-form>
        <el-form-item label="角色名称" prop="name" verify>
          <el-input
            v-model="form.data.name"
            clearable
            maxlength="20"
            placeholder="请输入角色名称"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark" verify>
          <el-input
            v-model="form.data.remark"
            clearable
            maxlength="20"
            placeholder="请输入备注"
            show-word-limit
          />
        </el-form-item>
      </template>
    </KiFormDialog>

    <KiFormDialog
      :show.sync="permission.show"
      title="后台功能授权"
      v-model="permission.data"
      :retrieve="permissionDataGetter"
      :submit="permissionSubmit"
    >
      <AuthTree v-model="permission.data.permissionTree" />
    </KiFormDialog>
  </div>
</template>

<script>
import pageMixin from '@/utils/pageMixin'
import useAdmateAdapter from '@/utils/useAdmateAdapter'
import AuthTree from '@/components/AuthTree/index.vue'

export default {
  mixins: [pageMixin],
  components: { AuthTree },
  setup: () =>
    useAdmateAdapter({
      urlPrefix: 'sot-admin-api/sys/company-role',
      axiosConfig: {
        getList: {
          url: 'queryForList'
        },
        d: {
          method: 'DELETE'
        }
      },
      list: {
        dataAt: 'data'
      }
    }),
  data () {
    return {
      permission: {
        show: false,
        data: {
          permissionTree: []
        }
      }
    }
  },
  methods: {
    adminEmpower (row) {
      this.permission.data = {
        ...this.permission.data,
        ...this.$lo.cloneDeep(row)
      }
      this.permission.show = true
    },
    permissionDataGetter () {
      return this.$GET('sot-admin-api/sys/company-role/resourceForSelect', {
        domainId: import.meta.env.VITE_APP_domainId,
        roleId: this.permission.data.roleId
      }).then(res => {
        if (res.errorCode == '00000' && res.data) {
          this.permission.data.permissionTree = res.data
        }
      })
    },
    permissionSubmit () {
      return this.$POST('sot-admin-api/sys/company-role/authorize', {
        roleId: this.permission.data.roleId,
        permissions: this.permission.data.permissionTree
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
